home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLINC.PAK / BITSET.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  5KB  |  224 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.6  $
  6. //
  7. // Definition of a bit set and a character set
  8. //----------------------------------------------------------------------------
  9. #if !defined(OWL_BITSET_H)
  10. #define OWL_BITSET_H
  11.  
  12. #if !defined(OWL_DEFS_H)
  13. # include <owl/defs.h>
  14. #endif
  15.  
  16. #if defined(BI_NAMESPACE)
  17. namespace ClassLib {
  18. #endif
  19.  
  20. class _BIDSCLASS opstream;
  21. class _BIDSCLASS ipstream;
  22.  
  23. #if defined(BI_NAMESPACE)
  24. } // namespace ClassLib
  25. #endif
  26.  
  27. #if defined(BI_NAMESPACE)
  28. namespace OWL {
  29. #endif
  30.  
  31. // Generic definitions/compiler options (eg. alignment) preceeding the 
  32. // definition of classes
  33. #include <services/preclass.h>
  34.  
  35. //
  36. // class TBitSet
  37. // ~~~~~ ~~~~~~~
  38. // Simplifies setting and testing bits in a 32 count array of uint8 (32 bytes).
  39. //
  40. class _OWLCLASS TBitSet {
  41.   public:
  42.     TBitSet();
  43.     TBitSet(const TBitSet& src);
  44.  
  45.     int Has(uint8 item) const;
  46.  
  47.     TBitSet operator ~() const;
  48.  
  49.     void DisableItem(uint8 item);
  50.     void EnableItem(uint8 item);
  51.     TBitSet& operator +=(uint8 item);
  52.     TBitSet& operator -=(uint8 item);
  53.  
  54.     void DisableItem(const TBitSet& bs);
  55.     void EnableItem(const TBitSet& bs);
  56.     TBitSet& operator +=(const TBitSet& bs);
  57.     TBitSet& operator |=(const TBitSet& bs);
  58.     TBitSet& operator -=(const TBitSet& bs);
  59.     TBitSet& operator &=(const TBitSet& bs);
  60.  
  61.     int TBitSet::IsEmpty() const;
  62.  
  63.     friend TBitSet operator &(const TBitSet& bs1, const TBitSet& bs2);
  64.     friend TBitSet operator |(const TBitSet& bs1, const TBitSet& bs2);
  65.  
  66.     friend int operator ==(const TBitSet& bs1, const TBitSet& bs2);
  67.     friend int operator !=(const TBitSet& bs1, const TBitSet& bs2);
  68.  
  69.     friend opstream& operator <<(opstream& out, const TBitSet& bs);
  70.     friend ipstream& operator >>(ipstream& in, TBitSet& bs);
  71.  
  72.   private:
  73.     int    Loc(uint8 item) const;
  74.     uint8  Mask(uint8 item) const;
  75.  
  76.     static uint8 near Masks[8];
  77.  
  78.     uint8  Bits[32];
  79. };
  80.  
  81. //
  82. // class CharSet
  83. // ~~~~~ ~~~~~~~~
  84. class _OWLCLASS TCharSet : public TBitSet {
  85.   public:
  86.     TCharSet();
  87.     TCharSet(const TBitSet& src);
  88.     TCharSet(const char far* str);
  89. };
  90.  
  91. //
  92. // TBitFlags is a *lightweight* class for setting, clearing and querrying
  93. // bit flags. It's intenteded to be used with a 'short' or 'long' type
  94. // allowing an easy method to handle 16 and 32 bit flags respectively.
  95. //
  96. // For example:
  97. //
  98. //    class TMyClass : public TMyBase, public TBitFlags<short> {
  99. //
  100. template <class T> class _OWLCLASS TBitFlags {
  101.   public:
  102.     TBitFlags(T  t = 0);
  103.  
  104.     // Query, Clear and Set flag bits
  105.     //
  106.     T       Clear(T t);
  107.     T       Set(T t);
  108.     bool    IsSet(T t) const;
  109.  
  110.   protected:
  111.     T       Bits;
  112. };
  113.  
  114. // Generic definitions/compiler options (eg. alignment) following the 
  115. // definition of classes
  116. #include <services/posclass.h>
  117.  
  118. #if defined(BI_NAMESPACE)
  119. } // namespace OWL
  120. #endif
  121.  
  122. //----------------------------------------------------------------------------
  123. // Inline implementations
  124. //
  125.  
  126. //
  127. // Enable the bit
  128. inline TBitSet& TBitSet::operator +=(uint8 item)
  129. {
  130.   EnableItem(item);
  131.   return *this;
  132. }
  133.  
  134. //
  135. inline TBitSet& TBitSet::operator -=(uint8 item)
  136. {
  137.   DisableItem(item);
  138.   return *this;
  139. }
  140.  
  141. //
  142. inline TBitSet& TBitSet::operator +=(const TBitSet& bs)
  143. {
  144.   EnableItem(bs);
  145.   return *this;
  146. }
  147.  
  148. //
  149. inline TBitSet& TBitSet::operator |=(const TBitSet& bs)
  150. {
  151.   EnableItem(bs);
  152.   return *this;
  153. }
  154.  
  155. //
  156. inline TBitSet& TBitSet::operator -=(const TBitSet& bs)
  157. {
  158.   DisableItem(bs);
  159.   return *this;
  160. }
  161.  
  162. //
  163. // Return the index of the item searched for.
  164. //
  165. inline int TBitSet::Loc(uint8 item) const {
  166.   return item / 8;
  167. }
  168.  
  169. //
  170. // Return the mask needed to look for a specific bit.
  171. //
  172. inline uint8 TBitSet::Mask(uint8 item) const {
  173.   return Masks[item & 0x07];
  174. }
  175.  
  176. //
  177. // Return true if the bitsets are not identical.
  178. // (Return the negative of operator ==).
  179. //
  180. inline int operator !=(const TBitSet& bs1, const TBitSet& bs2)
  181. {
  182.   return !operator ==(bs1, bs2);
  183. }
  184.  
  185. //
  186. // Initialize the Flags to the specified parameter
  187. //
  188. template <class T>
  189. TBitFlags<T>::TBitFlags(T t)
  190. :
  191.   Bits(t)
  192. {
  193. }
  194.  
  195. //
  196. // Activate the bits that are enabled in the specified parameter
  197. //
  198. template <class T>
  199. T TBitFlags<T>::Set(T t)
  200. {
  201.   return Bits |= t;
  202. }
  203.  
  204. //
  205. // Clear the bits that are enabled in the specified parameter
  206. //
  207. template <class T>
  208. T TBitFlags<T>::Clear(T t)
  209. {
  210.   return Bits &= ~t;
  211. }
  212.  
  213. //
  214. // Return true of the ON bits of the parameter are currently enabled.
  215. // Return false otherwise.
  216. //
  217. template <class T>
  218. bool TBitFlags<T>::IsSet(T t) const
  219. {
  220.   return (Bits & t) != 0;
  221. }
  222.  
  223. #endif  // OWL_BITSET_H
  224.